#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <strings.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

//nc -u -l 172.27.1.12 60000
//nc -u 172.27.1.12 50000
int main(int argc, const char *argv[])
{
    int tcp_socket;
    struct sockaddr_in peeraddr;
    char buf = 0;
    int ret_read;
    int ret_conn;
    int fd;

    fd = open("/home/jingwu/Pictures/htop.png", O_RDONLY);
    if(fd < 0) {
        perror("fail to open");
        exit(1);
    }

    int len;
    tcp_socket = socket(AF_INET, SOCK_STREAM, 0);
    if(tcp_socket < 0) {
        perror("fail to socket");
        exit(1);
    }

    peeraddr.sin_family = AF_INET;
    peeraddr.sin_port = htons(50000);
    peeraddr.sin_addr.s_addr = inet_addr("172.27.1.12");

    ret_conn = connect(tcp_socket, (struct sockaddr*)&peeraddr, sizeof(peeraddr));
    if(ret_conn == -1) {
        perror("fail to connect");
        exit(1);
    }
    while(1) {
        ret_read = read(fd, &buf, sizeof(buf));
        if(ret_read < 0) {
            perror("fail to read");
            exit(1);
        } else if(ret_read == 0) {
            break;
        } else {
            send(tcp_socket, &buf, sizeof(buf), 0);
        }
    }
    close(fd);
    close(tcp_socket);

    return 0;
}

